<template>
  <div
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    class="mt20"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      ref="excelTable"
      :merge="mergeArr"
      :column="tableData.column"
      :data="tableData.data"
      border
      :height="600"
      pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 30, 40]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
  </div>
</template>

  <script>
import { exportExcel } from "@/utils/xlsx";
import { deepClone, dealElectricity } from "@/utils/index";
import CascaderSelect from "@/components/Select/CascaderSelect";
import { energyStatisticYear } from "@/api/statistics/unit.js";

export default {
  name: "overYear",
  components: {
    CascaderSelect,
  },
  dicts: [],
  data() {
    return {
      mergeArr: ["year"],
      areaProps: {
        value: "id",
        children: "children",
        label: "label",
        checkStrictly: true,
        expandTrigger: "hover",
      },
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            startYear: {
              label: "起始年份:",
              typeName: "el-date-picker",
              type: "year",
              size: "small",
              "value-format": "yyyy",
              placeholder: "请选择起始年份",
              clearable: false,
              "picker-options": {
                disabledDate(time) {
                  let date = new Date();
                  return time.getFullYear() > date.getFullYear();
                },
              },
            },
            endYear: {
              label: "结束年份:",
              typeName: "el-date-picker",
              type: "year",
              size: "small",
              "value-format": "yyyy",
              placeholder: "请选择结束年份",
              clearable: false,
              "picker-options": {
                disabledDate(time) {
                  let date = new Date();
                  return time.getFullYear() > date.getFullYear();
                },
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            // {
            //   label: "导出",
            //   typeName: "el-button",
            //   plain: true,
            //   type: "warning",
            //   size: "small",
            //   icon: "el-icon-download",
            //   click: this.handleExport,
            // },
          ],
        },
      },
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: {
        column: [
          {
            prop: "year",
            label: "年份",
            width: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "deptName",
            label: "单位名称",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "roomArea",
            label: "用能面积(㎡)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "㎡";
              let value = scope.row.roomArea ?? 0;
              value = dealElectricity(value, 1, 2);
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          {
            prop: "userNum",
            label: "用能人数",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "roomNum",
            label: "房间数量",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "totalFee",
            label: "总费用(元)",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "元";
              let totalElectric = scope?.row?.totalFee ?? 0;
              let value;
              if (scope) {
                value = dealElectricity(totalElectric / 1, 1, 2);
              } else {
                value = "0.00";
              }
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          {
            prop: "",
            label: "人均电耗(kwh)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "kwh";
              let totalElectric = scope?.row?.perCapitaElectricConsumption ?? 0;
              let userNum = scope?.row?.userNum ?? 0;
              let value;
              if (scope) {
                value = dealElectricity(totalElectric / 1, 1, 2);
              } else {
                value = "0.00";
              }
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          {
            prop: "",
            label: "人均水耗(t)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "t";
              let totalWater = scope?.row?.perCapitaWaterConsumption ?? 0;
              let userNum = scope?.row?.userNum ?? 0;
              let value;
              if (scope) {
                value = dealElectricity(totalWater / 1, 1, 2);
              } else {
                value = "0.00";
              }
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          // {
          //   prop: "",
          //   label: "人均碳排(kgCO2)",
          //   minWidth: 100,
          //   align: "center",
          //   showOverflowTooltip: true,
          //   render: (h, scope) => {
          //     let unit = "kgce";
          //     let totalCarbon = scope?.row?.perCapitaCarbonEmissions ?? 0;
          //     let userNum = scope?.row?.userNum ?? 0;
          //     let value;
          //     if (scope) {
          //       value = dealElectricity(totalCarbon / 1, 1, 2);
          //     } else {
          //       value = "0.00";
          //     }
          //     let a = value + unit;
          //     return <div>{value}</div>;
          //   },
          // },
          {
            prop: "totalElectricConsumption",
            label: "总电耗(kwh)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "kwh";
              let value = scope?.row?.totalElectricConsumption ?? 0;
              value = dealElectricity(value, 1, 2);
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          {
            prop: "totalWaterConsumption",
            label: "总水耗(t)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "t";
              let value = scope?.row?.totalWaterConsumption ?? 0;
              value = dealElectricity(value, 1, 2);
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          {
            prop: "totalCarbonEmissions",
            label: "总碳排(kgCO2)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "kgce";
              let value = scope?.row?.totalCarbonEmissions ?? 0;
              value = dealElectricity(value, 1, 2);
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          {
            prop: "publicShareElectricConsumption",
            label: "公摊用电(kwh)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "kwh";
              let value = scope?.row?.publicShareElectricConsumption ?? 0;
              value = dealElectricity(value, 1, 2);
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          {
            prop: "publicShareWaterConsumption",
            label: "公摊用水(t)",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let unit = "t";
              let value = scope?.row?.publicShareWaterConsumption ?? 0;
              value = dealElectricity(value, 1, 2);
              let a = value + unit;
              return <div>{value}</div>;
            },
          },
          // {
          //   prop: "totalFee",
          //   label: "实际费用",
          //   minWidth: 100,
          //   align: "center",
          //   showOverflowTooltip: true,
          //   render: (h, scope) => {
          //     let unit = "元";
          //     let value = scope?.row?.totalFee ?? 0;
          //     value = dealElectricity(value, 1, 2);
          //     let a = value + unit;
          //     return <div>{a}</div>;
          //   },
          // },
          {
            label: "操作",
            width: "120",
            fixed: "right",
            render: (h, scope) => {
              let that = this;
              let a = (
                <el-button
                  type="text"
                  size="small"
                  icon="el-icon-view"
                  onClick={() => {
                    that.$router.push({
                      name: "statisticsUnitDetail",
                      query: {
                        deptId: scope.row.deptId,
                        year: scope.row.year,
                        deptName: scope?.row?.deptName ?? "未知部门",
                        overYear: 1,
                      },
                    });
                  }}
                >
                  详情
                </el-button>
              );
              return <div>{a}</div>;
            },
          },
        ],
        data: [],
      },
    };
  },
  created() {},
  mounted() {
    this.choseFullYear();
  },
  methods: {
    handleExport() {
      if (this.tableData.data.length <= 0) {
        this.$message.warning("无内容导出,请确认!");
        return false;
      }
      let fileName = "单位用能统计";
      let cols = [{ wpx: 85 }, { wpx: 240 }];
      exportExcel(this.$refs.excelTable, fileName, cols);
    },
    choseFullYear() {
      let year = new Date().getFullYear();
      let startYear = year - 2;
      year = String(year);
      startYear = String(startYear);
      this.$nextTick(() => {
        this.$refs?.form?.setFieldValue("startYear", startYear);
        this.$refs?.form?.setFieldValue("endYear", year);
        this.handlerSearchDetail();
      });
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await energyStatisticYear(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      this.initData(data);
    },
    // 重置
    handleReset() {
      // this.$refs?.form?.setFieldValue("deptId", null);
      this.$refs?.form?.resetFields();
      this.choseFullYear();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
  },
};
</script>

  <style lang="scss" scoped>
.tag-area {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  .el-tag {
    margin-right: 10px;
  }
}
</style>
